home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / Font Handler / MakeFontAvailable.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  9.0 KB  |  348 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        MakeFontAvailable.c
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.      
  6.                          File contains routines for MakeFontAvailable:
  7.     
  8.                         This is the function which generates the PostScript
  9.                         font name for the style given.
  10.  
  11.      Version:    Technology:    Quickdraw GX 1.1.x
  12.       
  13.      Copyright:    © 1991-1997 by Apple Computer, Inc., all rights reserved.
  14. */
  15.  
  16. #include "GXToPSBuildConfig.h"
  17. #include <GXExceptions.h>
  18. #include "GXPrintingUniverse.h"
  19. #include "FontHandler.h"
  20. #include "FontHandlerPrivate.h"
  21. #include "FontHandlerVariations.h"
  22.  
  23. #include "FHResources.h"
  24. #include <MacMemory.h>
  25.  
  26.  
  27. /*************************************************
  28.     
  29.     Function: FHRestoreSaveLevelFonts
  30.     
  31.     Function marks all fonts in the current save level
  32.     as unavailable.  This should be done whenever a 
  33.     restore is done.
  34.     
  35. **************************************************/
  36. OSErr FHRestoreSaveLevelFonts(TFontHandlerHdl hFHRec);
  37. OSErr FHRestoreSaveLevelFonts(TFontHandlerHdl hFHRec)
  38.     {
  39.         OSErr                                status;
  40.         long                                i, n, offset;
  41.         TPrinterFontRec            *printerFont;
  42.         TFontHandlerPtr            pFHRec;
  43.         
  44.         /** Mark all fonts that were in the save level as unavailable and reset the VM value **/
  45.         
  46.         n = (*hFHRec)->numPrFonts;
  47.         
  48.         for (i = 1; i <= n; ++i) {
  49.         
  50.             status = GetIndexedCollectionItem((*hFHRec)->printerFontsOffsets, i, nil, &offset);
  51.             nrequire(status, failed_GetItem);
  52.             
  53.             pFHRec = *hFHRec;            // Caller locked this.
  54.             
  55.             printerFont = (TPrinterFontRec*)(pFHRec->printerFonts + offset);
  56.             if (printerFont->info & fontIsInSaveLevel) {
  57.             
  58.                 printerFont->info &= ~(fontIsEncoded | fontIsInSaveLevel | fontIsDownloaded);
  59.                 pFHRec->vmAvailable += printerFont->vmUsage;
  60.                 
  61.             }//end if
  62.             
  63.         }//end for
  64.         
  65. failed_GetItem:
  66.         return(status);
  67.     
  68.     }//FHRestoreSaveLevelFonts
  69.  
  70.  
  71.  
  72. //<FF>
  73. /**************************************************
  74.  
  75.     FHDownloadPageLevelFont:
  76.     
  77.     Routine downloads a font at the page-level.
  78.     
  79.     hFHRec:                        handle to the font handler record.  (Assumed locked)
  80.     printerFont:            the printer font to download.
  81.     saveLevelResult:    the save level result.
  82.     
  83. ***************************************************/
  84. OSErr FHDownloadPageLevelFont(TFontHandlerHdl hFHRec, TPrinterFontRec *printerFont,
  85.                                                                 TFHSaveResult *saveLevelResult);
  86. OSErr FHDownloadPageLevelFont(TFontHandlerHdl hFHRec, TPrinterFontRec *printerFont,
  87.                                                                 TFHSaveResult *saveLevelResult)
  88.     {
  89.         OSErr                                status, saveStatus;
  90.         TFontHandlerPtr            pFHRec = *hFHRec;
  91.         TRDParams                        rdParams;
  92.         
  93.         rdParams.rdMap = pFHRec->rdMap;
  94.         rdParams.resType = kScriptResType;
  95.         rdParams.resID = kFHScriptResID;
  96.         rdParams.rdFlags = eRDnoOptions;
  97.         
  98.         /** Put font handler dictionary on dict stack **/
  99.         
  100.         rdParams.resIndex = kFHDictOnStack;
  101.         nrequire(status = RDResPrintf(&rdParams), failed_DictOn);
  102.         
  103.  
  104.         /**** Establish the correct save-level ****/
  105.         
  106.         if (!pFHRec->saveLevel) {
  107.         
  108.             rdParams.resIndex = kDoSave;
  109.             nrequire(status = RDResPrintf(&rdParams), failed_Save);
  110.             
  111.             pFHRec->saveLevel = true;
  112.             
  113.             *saveLevelResult = fhDidSave;
  114.         
  115.         } else {        /*** We already did a save ***/
  116.  
  117.             /*******
  118.                 If there isn't enough memory, just download the font
  119.                 Do a restore/save to flush old fonts that are in save-level
  120.             ********/
  121.             if (printerFont->vmUsage > pFHRec->vmAvailable) {
  122.             
  123.                 rdParams.resIndex = kDoRestoreSave;
  124.                 nrequire(status = RDResPrintf(&rdParams), failed_RestoreSave);
  125.                 
  126.                 *saveLevelResult = fhDidRestoreSave;
  127.                 
  128.                 status = FHRestoreSaveLevelFonts(hFHRec);
  129.                 nrequire(status, failed_RestoreFonts);
  130.             
  131.             } else {
  132.             
  133.                 *saveLevelResult = fhNoChange;
  134.             
  135.             }//end if
  136.                     
  137.         }//end if
  138.  
  139.         /*******
  140.             Make the font available and ready for use:
  141.                 If it is a printer-resident font download an encoding
  142.                 If not, download a partial font as specified.
  143.         *******/
  144.         
  145.         if (printerFont->vmUsage <= pFHRec->vmAvailable) {
  146.     
  147.             if (printerFont->info & fontIsInPrinter) {
  148.             
  149.                 /*******
  150.                     The only time we would re-encode a resident font at the page level
  151.                     is when an extension added text that used a resident font but the
  152.                     document norally didn't encode the glyphs used
  153.                 *******/
  154.                 status = FHReEncodeFont(pFHRec, printerFont, nil);
  155.                 nrequire(status, failed_ReEncode);
  156.                 
  157.             } else {
  158.             
  159.                 status = FHDownloadFont(pFHRec, printerFont, pFHRec->legalStreamTypes, pFHRec->productDescription);
  160.                 nrequire(status, failed_Download);
  161.                 
  162.             }//end if
  163.             
  164.             pFHRec->vmAvailable -= printerFont->vmUsage;
  165.             printerFont->info |= fontIsInSaveLevel;
  166.         
  167.         } else {
  168.         
  169.             /**********
  170.             
  171.                 The printer did not have enough memory for the font request
  172.                 
  173.             **********/
  174.             status = gxNotEnoughPrinterMemory;
  175.             #if DEBUGLEVEL > 1
  176.             dprintf(notrace, "Fatal Error, no memory for font: %d, takes: %d, remaining: %d",
  177.                                 printerFont->theFont, printerFont->vmUsage, pFHRec->vmAvailable);
  178.             #endif
  179.             nrequire(status, failed_NoMemory);
  180.             
  181.         }//end if
  182.         
  183.         #if DEBUGLEVEL > 1
  184.         rdParams.resIndex = kDebugShowVMLeft;
  185.         status = RDResPrintf(&rdParams, pFHRec->vmAvailable);
  186.         #endif
  187.  
  188. failed_NoMemory:
  189. failed_RestoreFonts:
  190. failed_GetItem:
  191. failed_Download:
  192. failed_ReEncode:
  193. failed_RestoreSave:
  194. failed_Save:
  195.  
  196.         /** Remove font handler dictionary from dict stack **/
  197.  
  198.         rdParams.resIndex = kFHDictOffStack;
  199.         saveStatus = RDResPrintf(&rdParams);
  200.         if (status == noErr)
  201.             status = saveStatus;        
  202.  
  203. failed_DictOff:
  204. failed_DictOn:
  205.  
  206.         return(status);
  207.         
  208.     }//FHDownloadPageLevelFont
  209.  
  210. //<FF>
  211. /*****************************************
  212.  
  213.     Function FontHandlerMakeFontAvailable:
  214.     
  215.     This function takes a style that has been 
  216.     tagged by ResolveShapeFonts and genrates a PostScript
  217.     name for it.  It is also responsible for seeing to
  218.     it that the font is actually availabe on the printer
  219.     by downloading or rencoding as necessary.
  220.     
  221.     context:                    A font handler context.
  222.     theStyle:                    Style to translate to PostScript.
  223.     prFontIndex:            (returned)  The index of the printer font.  
  224.                                                 (This combined with font ID gives unique printer font)
  225.     name:                            The PostScript name to use.  Pascal string.
  226.     saveLevelResult:    The result of font-handler saving and restoring.
  227.     
  228. *******************************************/
  229. OSErr    FontHandlerMakeFontAvailable(TFontHandlerContext context, gxStyle theStyle, long *prFontIndex,
  230.                                                                         unsigned char name[256], TFHSaveResult *saveLevelResult)
  231.     {
  232.         OSErr                                        status;    
  233.         fhFont                                    theFont;
  234.         long                                        nameLen;
  235.         TPrinterFontRec                    *printerFont;
  236.         long                                        offset;
  237.         
  238.         /** Get the font index information from the style **/
  239.         
  240.         *prFontIndex = FHGetStylePrFontIndex(theStyle);
  241.         
  242.         status = _FontHandlerGetStyleFont(context, theStyle, &theFont);
  243.         nrequire(status, failed_fhgetfont);
  244.                 
  245.         /** Get printer-font record for this font and index **/
  246.         
  247.         status = FHGetIndexedPrinterFont((TFontHandlerHdl)context, theFont, *prFontIndex, 
  248.                                                                                 &printerFont, &offset);
  249.         nrequire(status, failed_GetIndexed);
  250.  
  251.         /*** If the font is not available, make it so ***/
  252.         
  253.         if (!(printerFont->info & fontIsEncoded)) {
  254.  
  255.             HLockHi((Handle)context);
  256.             
  257.             printerFont = (TPrinterFontRec*)((*(TFontHandlerHdl)context)->printerFonts + offset);            // Point at the printer font again.
  258.  
  259.             status = FHDownloadPageLevelFont((TFontHandlerHdl)context, printerFont, saveLevelResult);
  260.             
  261.             HUnlock((Handle)context);
  262.             
  263.             nrequire(status, failed_PageLevel);
  264.  
  265.         } else {
  266.         
  267.             *saveLevelResult = fhNoChange;
  268.         
  269.         }//end if
  270.                 
  271.         /** Generate the PostScript name for the font - as a Pascal String **/
  272.  
  273.         if (printerFont->info & fontIsTwoByteMember)
  274.             nameLen = FHGenerateTwoByteFontName((TFontHandlerHdl)context, printerFont->theFont, &name[1]);
  275.         else
  276.             nameLen = FHGeneratePrinterName(printerFont, &name[1]);
  277.             
  278.         name[0] = nameLen;
  279.  
  280. failed_PageLevel:        
  281. failed_FontName:
  282. failed_GetIndexed:
  283. failed_GetStyleTag:
  284. failed_fhgetfont:
  285.  
  286.         return(status);
  287.         
  288.     }//FontHandlerMakeFontAvailable
  289.  
  290.  
  291.  
  292.  
  293. //<FF>
  294. /*************************************
  295.  
  296.     Function: FontHandlerBalanceSaveLevel
  297.     
  298.     Function restore's the font handler's save
  299.     level and marks any fonts in that save-level
  300.     as unavailable.
  301.     
  302. **************************************/
  303. OSErr FontHandlerBalanceSaveLevel(TFontHandlerContext context)
  304.     {
  305.         OSErr                                status;
  306.         TFontHandlerPtr            pFHRec;
  307.         TRDParams                        rdParams;
  308.         
  309.         pFHRec = *(TFontHandlerHdl)context;
  310.         
  311.         pFHRec->saveLevel = false;                            // turn off that a save is pending.
  312.         
  313.         
  314.         rdParams.rdMap = pFHRec->rdMap;
  315.         rdParams.resType = kScriptResType;
  316.         rdParams.resID = kFHScriptResID;
  317.         rdParams.rdFlags = eRDnoOptions;
  318.         
  319.         /** Put font handler dictionary on dict stack **/
  320.         
  321.         rdParams.resIndex = kFHDictOnStack;
  322.         nrequire(status = RDResPrintf(&rdParams), failed_DictOn);
  323.         
  324.         
  325.         /*** output the restore ***/
  326.  
  327.         rdParams.resIndex = kDoRestore;
  328.         nrequire(status = RDResPrintf(&rdParams), failed_Restore);
  329.         
  330.         
  331.         /** Remove font handler dictionary from dict stack **/
  332.         
  333.         rdParams.resIndex = kFHDictOffStack;
  334.         nrequire(status = RDResPrintf(&rdParams), failed_DictOff);
  335.         
  336.         
  337.         /** Mark all fonts in list that were in save level as unavailable **/
  338.         
  339.         status = FHRestoreSaveLevelFonts((TFontHandlerHdl)context);
  340.         ncheck(status);        
  341.  
  342. failed_DictOff:
  343. failed_Restore:
  344. failed_DictOn:
  345.  
  346.         return(status);
  347.         
  348.     }//FontHandlerBalanceSaveLevel